#include "MRenderQueueGroup.h"


CMRenderQueueGroup::CMRenderQueueGroup( CMRenderQueue* pQueue , RenderQueueGroupID eGroupID )
: m_pRenderQueue( pQueue ) ,
m_eGroupID( eGroupID )
{
	CMRenderPriorityGroup* pPriorityGrp = new CMRenderPriorityGroup( this );

	m_PriorityGroups.insert( PriorityMap::value_type( RENDERABLE_DEFAULT_PRIORITY , pPriorityGrp ) );
}

CMRenderQueueGroup::~CMRenderQueueGroup( void )
{
	// destroy contents now
	PriorityMap::iterator i;

	for ( i = m_PriorityGroups.begin() ; i != m_PriorityGroups.end() ; ++i )
	{
		SAFE_DELETE(i->second);
	}
}

void CMRenderQueueGroup::Clear( bool bDestroy )
{
	PriorityMap::iterator i;
	PriorityMap::iterator iEnd = m_PriorityGroups.end();

	for ( i = m_PriorityGroups.begin() ; i != iEnd ; ++i )
	{
		if ( bDestroy )
		{
			delete i->second;
		}
		else
		{
			i->second->Clear();
		}
	}

	if ( bDestroy )
	{
		m_PriorityGroups.clear();
	}
}

void CMRenderQueueGroup::AddRenderable( CMRenderable* pRenderable , CMTechnique* pTechnique , unsigned int uiPriority )
{
	// Check if priority group is there
	PriorityMap::iterator i = m_PriorityGroups.find( uiPriority );
	CMRenderPriorityGroup* pPriorityGrp;

	if ( i == m_PriorityGroups.end() )
	{
		// Missing, create
		pPriorityGrp = new CMRenderPriorityGroup( this );
		m_PriorityGroups.insert( PriorityMap::value_type( uiPriority , pPriorityGrp ) );
	}
	else
	{
		pPriorityGrp = i->second;
	}

	// Add
	pPriorityGrp->AddRenderable( pRenderable , pTechnique );
}